package com.ebt.m.proposal_v2.widget.dialog;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ebt.m.R;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author archer.qi
 *         Created on 2017/5/16.
 */
public abstract class EBTDialogBase extends DialogFragment {
    public static final String Arg_Data = "Data";


    private View mContentView;

    @BindView(R.id.EBTDialogHeader)
    TextView mDialogHeader;
    @BindView(R.id.EBTDialogHeader_divider)
    View mHeaderDivider;

    @BindView(R.id.EBTDialogFooter)
    LinearLayout mDialogFooter;
    @BindView(R.id.EBTDialogFooter_negative)
    Button mFooterBtnNegative;
    @BindView(R.id.EBTDialogFooter_positive)
    Button mFooterBtnPositive;
    @BindView(R.id.EBTDialogFooter_divider)
    View mFooterDivider;
    @BindView(R.id.EBTDialogFooter_button_middle_line)
    View mFooterMiddleLine;

    @BindView(R.id.EBTDialogBody)
    LinearLayout mDialogContainer;

    private String mDialogTitle;
    private String mDialogNegativeText;
    private View.OnClickListener mDialogNegativeListener;
    private String mDialogPositiveText;
    private View.OnClickListener mDialogPositiveListener;
    private boolean isBottomHidden;
    private boolean isTitleHidden;
    private boolean isBodyHidden;
    private boolean isSingleButton;
    private View mContainerChildView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (mContentView == null) {
            getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
            mContentView = inflater.inflate(R.layout.dialog_ebt, container, false);
            ButterKnife.bind(this, mContentView);
            init();
            bindData();
        }
        if (mContentView != null) {
            ViewGroup parent = (ViewGroup) mContentView.getParent();
            if (parent != null) {
                parent.removeAllViews();
            }
        }
        return mContentView;
    }

    private void bindData() {
        if (isTitleHidden) {
            mDialogHeader.setVisibility(View.GONE);
            mDialogContainer.setBackgroundResource(R.drawable.bg_dialog_header_white);
            mHeaderDivider.setVisibility(View.GONE);
        } else {
            if (mDialogTitle != null && mDialogTitle.length() > 0) {
                mDialogHeader.setText(mDialogTitle);
            } else {
                mDialogHeader.setVisibility(View.GONE);
            }
        }

        if (isBottomHidden) {
            mDialogFooter.setVisibility(View.GONE);
            mFooterDivider.setVisibility(View.GONE);
        } else {
            if (mDialogNegativeText != null && mDialogNegativeText.length() > 0) {
                mFooterBtnNegative.setText(mDialogNegativeText);
            }
            if (mDialogNegativeListener != null) {
                mFooterBtnNegative.setOnClickListener(mDialogNegativeListener);
            }
            if (mDialogPositiveText != null && mDialogPositiveText.length() > 0) {
                mFooterBtnPositive.setText(mDialogPositiveText);
            }
            if (mDialogPositiveListener != null) {
                mFooterBtnPositive.setOnClickListener(mDialogPositiveListener);
            }
            if (isSingleButton) {
                mFooterMiddleLine.setVisibility(View.GONE);
                mFooterBtnNegative.setVisibility(View.GONE);
                mFooterBtnPositive.setBackgroundResource(R.drawable.selector_bottom_button);
            }
        }

        if (mContainerChildView != null) {
            mDialogContainer.removeAllViews();
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
            mDialogContainer.addView(mContainerChildView, params);
        }

        if (isBodyHidden) {
            mDialogContainer.setVisibility(View.GONE);
            mHeaderDivider.setVisibility(View.GONE);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        resizeDialog();
    }

    @Override
    public void onStart() {
        super.onStart();
        removeBorder();
    }

    /**
     * 设定DIALOG大小与位置
     */
    private void resizeDialog() {
        Dialog mDialog = getDialog();
        WindowManager.LayoutParams p = mDialog.getWindow().getAttributes();
        p.width = WindowManager.LayoutParams.WRAP_CONTENT;
        p.height = WindowManager.LayoutParams.WRAP_CONTENT;
        p.gravity = Gravity.CENTER;
        mDialog.getWindow().setAttributes(p);
        mDialog.getWindow().setBackgroundDrawable(null);
        mDialog.setCancelable(false);
        mDialog.setCanceledOnTouchOutside(false);
    }

    /**
     * 去边框
     */
    private void removeBorder() {
        Dialog dialog = getDialog();
        if (dialog != null) {
            dialog.getWindow().setLayout(-1, -2);
        }
    }

    public abstract void init();

    public void hideBottom() {
        isBottomHidden = true;
    }

    public void setNegativeButton(String text, View.OnClickListener listener) {
        mDialogNegativeText = text;
        mDialogNegativeListener = listener;
    }

    public void setPositiveButton(String text, View.OnClickListener listener) {
        mDialogPositiveText = text;
        mDialogPositiveListener = listener;
    }

    public void setTitle(String title) {
        mDialogTitle = title;
    }

    public void hideTitle() {
        isTitleHidden = true;
    }

    public void addDialogBody(View view) {
        mContainerChildView = view;
    }

    public void hideBody() {
        isBodyHidden = true;
    }

    public void setSingleButton(boolean single) {
        isSingleButton = single;
    }

    public View getmContentView() {
        return mContentView;
    }

}
